﻿using AntdUI;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TestPressure
{
    public partial class DevicePointAddForm : BaseForm
    {
        #region 属性

        private DevicePoint obj = new DevicePoint();

        /// <summary>设备
        /// 
        /// </summary>
        public DeviceItem deviceItem;

        #endregion

        #region 自定义方法

        /// <summary>重载
        /// 
        /// </summary>
        private void Reload()
        {
            PointNumberInput.Text = DateTimeOffset.Now.ToUnixTimeSeconds().ToString();
            PointNameInput.Text = "";
            PointAddressInput.Text = "";
            RangeMinInput.Text = "";
            RangeMaxInput.Text = "";
            OutMinInput.Text = "";
            OutMaxInput.Text = "";
            AmendInput.Text = "";
            AlarmHightInput.Text = "";
            AlarmLowInput.Text = "";
            ErrorRangeInput.Text = "";
            UnitInput.Text = "";
            RegisterTypeSelect.Text = "";
        }
        /// <summary>重复检测
        /// 
        /// </summary>
        /// <returns></returns>
        private bool CheckRepeat()
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["device_number"] = DeviceNumberInput.Text;
            where["point_name"] = PointNameInput.Text;
            List<DevicePoint> list = SqlSugarHelper.Inquireable<DevicePoint>(where);
            int count = list.Count;
            if (count > 0)
            {
                string msg = string.Format("【{0}】设备已存在【{1}】点位", deviceItem.deviceName, where["point_name"]);
                Error(msg, this);
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>根据主键查询设备
        /// 
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        private Device? DeviceWithPrimaryKey(int number)
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["device_number"] = number;
            List<Device> list = SqlSugarHelper.Inquireable<Device>(where);
            if (list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return null;
            }
        }

        private int RegisterTypeToInt(string typeString)
        {
            int type = 0;
            if (typeString == "DI")
            {
                type = 1;
            }
            else if (typeString == "DO")
            {
                type = 2;
            }
            else if (typeString == "Input")
            {
                type = 3;
            }
            else if (typeString == "Holding")
            {
                type = 4;
            }
            return type;
        }
        #endregion

        #region 系统方法
        public DevicePointAddForm()
        {
            InitializeComponent();
            Header.Text = "添加设备点位";
        }

        private void DevicePointAddForm_Load(object sender, EventArgs e)
        {
            Reload();
            if (deviceItem is not null)
            {
                Device? device = DeviceWithPrimaryKey(deviceItem.deviceNumber);
                if (device == null)
                {

                    if (this.Owner is not null)
                    {
                        DevicePointManageForm frm = (DevicePointManageForm)this.Owner;
                        frm.LoadList();
                        string msg = string.Format("【{0}】设备不存在，无法添加点位", deviceItem.deviceName);
                        frm.Error(msg, frm);
                        this.Close();
                    }
                }
                else
                {
                    if (device.device_name is not null)
                    {
                        DeviceNameInput.Text = device.device_name;
                    }

                    DeviceNumberInput.Text = device.device_number.ToString();
                    DeviceAddressInput.Text = device.device_address.ToString();
                }
            }
            GroupBox3.Visible = false;
        }
        #endregion

        #region 响应事件
        private void ConfirmButton_Click(object sender, EventArgs e)
        {
            if (PointNameInput.Text.Length == 0)
            {
                Error("请输入点位名称", this);
                PointNameInput.Focus();
                return;
            }
            obj.device_name = DeviceNameInput.Text;

            if (PointAddressInput.Text.Length == 0)
            {
                Error("请输入点位地址", this);
                PointAddressInput.Focus();
                return;
            }
            obj.device_address = OutMaxInput.Text.ObjToInt();

            if (RegisterTypeSelect.Text.Length == 0)
            {
                Error("请选择存储类型", this);
                RegisterTypeSelect.Focus();
                return;
            }

            bool repeatRes = CheckRepeat();
            if (repeatRes)
            {
                return;
            }
            obj.device_number = DeviceNumberInput.Text.ObjToInt();
            obj.device_name = DeviceNameInput.Text;
            obj.device_address = DeviceAddressInput.Text.ObjToInt();
            obj.point_number = PointNumberInput.Text.ObjToInt();
            obj.point_name = PointNameInput.Text;
            obj.point_address = PointAddressInput.Text.ObjToInt();
            obj.range_max = RangeMaxInput.Text.ObjToInt();
            obj.range_min = RangeMinInput.Text.ObjToInt();
            obj.out_max = OutMaxInput.Text.ObjToInt();
            obj.out_min = OutMinInput.Text.ObjToInt();
            obj.amend = AmendInput.Text.ObjToInt();
            obj.alarm_hight = AlarmHightInput.Text.ObjToInt();
            obj.alarm_low = AlarmLowInput.Text.ObjToInt();
            obj.error_range = ErrorRangeInput.Text.ObjToInt();
            obj.unit = UnitInput.Text;
            obj.register_type = RegisterTypeToInt(RegisterTypeSelect.Text);
            List<DevicePoint> list = new List<DevicePoint>();
            list.Add(obj);
            int res = SqlSugarHelper.Insertable(list);
            if (res > 0)
            {
                Reload();
                if (this.Owner is not null)
                {
                    DevicePointManageForm frm = (DevicePointManageForm)Owner;
                    frm.LoadList();
                    string msg = string.Format("【{0}】设备添加点位成功", obj.device_name);
                    frm.Success(msg, frm);
                }
                this.Close();
            }
            else
            {
                Error("添加点位失败", this);
            }
        }

        private void RegisterTypeSelect_SelectedValueChanged(object sender, ObjectNEventArgs e)
        {
            if (RegisterTypeSelect.Text == "Input" || RegisterTypeSelect.Text == "Holding")
            {
                GroupBox3.Enabled = true;
                GroupBox3.Visible = true;
            }
            else
            {
                GroupBox3.Enabled = false;
                GroupBox3.Visible = false;
            }
        }

        #endregion
    }
}
